package com.person.monitor.helper;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;

import com.person.monitor.LogcatHelper;
import com.person.monitor.MainActivity;
import com.person.monitor.VolumeHelperActivity;

public class VolumeHelper {

    public static final String EXTRA_COMMAND = "extra_command";
    public static final String COMMAND_CLOSE_VOLUME = "command_close_volume";
    public static final String COMMAND_OPEN_VOLUME = "command_open_volume";
    public static final String COMMAND_ALARM_MAX = "command_alarm_max";

    int musicMaxVolume;
    int alarmMaxVolume;
    AudioManager audioManager;
    Context context;

    private VolumeHelper(Context context){
        this.context = context;

        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        musicMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        alarmMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    }

    public void closeVolume(){
        LogcatHelper.getInstance().printLog("closeVolume");
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_VIBRATE);
    }

    public void openVolume(){
        LogcatHelper.getInstance().printLog("openVolume");
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicMaxVolume/3, AudioManager.FLAG_VIBRATE);
    }

    public void checkAlarmVolume(){
        LogcatHelper.getInstance().printLog("checkAlarmVolume set alarm MaxVolume");
        audioManager.setStreamVolume(AudioManager.STREAM_ALARM, alarmMaxVolume, AudioManager.FLAG_VIBRATE);
    }

    public void startActivityCommand(String extraCommand){
        Intent intent = new Intent();
        intent.setClass(context, VolumeHelperActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(EXTRA_COMMAND, extraCommand);
        context.startActivity(intent);
    }

    private static VolumeHelper instance;

    public static void init(Context context){
        if(instance == null){
            instance = new VolumeHelper(context);
        }
    }

    public static VolumeHelper getInstance() {
        return instance;
    }


}
